package com.mondo.financialwallet.presentation.ui.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

import com.mondo.financialwallet.R;

import java.util.Arrays;
import java.util.List;

public class ProductLifecycleView extends View {

    private List<Data> dataList = Arrays.asList(
            new Data("起购日", "2019/01/23"),
            new Data("起息日", "2019/01/24"),
            new Data("到期日", "2019/01/25"),
            new Data("回款日", "2019/01/26")
    );

    // 节点半径
    private int mNodeRadius = dip2px(5);

    // 开始和结束的边距
    private int mStartAndEndSpace = dip2px(16);

    // 画笔
    private Paint mNodePaint, mLinePaint, mTitlePaint, mDatePaint;

    public ProductLifecycleView(Context context, AttributeSet attrs) {
        super(context, attrs);

        // 节点画笔
        mNodePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mNodePaint.setStyle(Paint.Style.FILL);
        mNodePaint.setColor(getResources().getColor(R.color.colorPrimary));

        // 线画笔
        mLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mLinePaint.setStyle(Paint.Style.FILL);
        mLinePaint.setColor(getResources().getColor(R.color.colorPrimary));
        mLinePaint.setStrokeWidth(dip2px(1));

        // 标题笔画
        mTitlePaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        mTitlePaint.setColor(getResources().getColor(R.color.colorPrimaryText));
        mTitlePaint.setTextSize(dip2px(14));

        // 时间画笔
        mDatePaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        mDatePaint.setColor(getResources().getColor(R.color.colorWeakText));
        mDatePaint.setTextSize(dip2px(11));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawLine(
                mStartAndEndSpace + getPaddingLeft(),
                getHeight() / 2,
                getWidth() - mStartAndEndSpace - getPaddingRight(),
                getHeight() / 2,
                mLinePaint
        );

        int size = dataList.size();
        for (int i = 0; i < size; i++) {
            Data data = dataList.get(i);

            if (i == 0){
                drawNode(canvas, 100 + getPaddingLeft());
                drawTitle(canvas, data.title, 100 + getPaddingLeft());
                drawDate(canvas, data.date, 100 + getPaddingLeft());
            } else if(i == size - 1){
                drawNode(canvas, getWidth() - 100 - getPaddingRight());
                drawTitle(canvas, data.title, getWidth() - 100 - getPaddingRight());
                drawDate(canvas, data.date, getWidth() - 100 - getPaddingRight());
            } else {
                int itemWidth = getWidth() / size;
                int x = i * itemWidth + itemWidth / 2;
                drawNode(canvas, x);
                drawTitle(canvas, data.title, x);
                drawDate(canvas, data.date, x);
            }
        }
    }

    private void drawNode(Canvas c, float cx) {
        c.drawCircle(
                cx,
                getHeight() / 2,
                mNodeRadius,
                mNodePaint
        );
    }

    private void drawTitle(Canvas c, String title, float x) {
        Rect rect = new Rect();
        mTitlePaint.getTextBounds(title, 0, title.length(), rect);
        c.drawText(title, x - rect.width() / 2, 40, mTitlePaint);
    }

    private void drawDate(Canvas c, String date, float x) {
        Rect rect = new Rect();
        mDatePaint.getTextBounds(date, 0, date.length(), rect);
        c.drawText(date, x - rect.width() / 2, getHeight() - 40, mDatePaint);
    }

    public void setData(List<Data> dataList) {
        this.dataList = dataList;
        requestLayout();
    }

    public int dip2px(float dp) {
        final float scale = getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }

    public static class Data {
        private String title;
        private String date;

        public Data(String title, String data) {
            this.title = title;
            this.date = data;
        }
    }
}
